<template>
	<app-layout>

		<search-top ref="search" :searchForm.sync="searchForm" @search="Search" :showtime="false">
			<!-- {{putin}} -->
			<view class="tn-bg-white topForm" v-if="putin['日报编号']">
				<tn-form class="tn-padding-sm" :labelWidth="150" :model="putin" ref="uForm" :errorType="['message']">
					<view class="tn-bg-white ">
						<view class="tn-bg-white ">
							<tn-form-item label="入库单" prop="order_no">
								<tn-input v-model="putin['日报编号']" type="string" disabled></tn-input>
							</tn-form-item>

							<tn-form-item label="入库日期" prop="date">
								<tn-input v-model="putin['日报日期']" type="string" disabled></tn-input>
							</tn-form-item>
							<tn-form-item label="制单人" prop="supplier">
								<tn-input v-model="putin['制单人']" type="string" disabled></tn-input>
							</tn-form-item>
							<!-- <tn-form-item label="工单号" prop="send_no">
								<tn-input v-model="putin['工单编号']" type="string" disabled></tn-input>
							</tn-form-item> -->
						</view>
					</view>
				</tn-form>
			</view>
		</search-top>

		<view class="nav-height" v-if="putin['日报编号']"> </view>
		<view class="list">
			<!-- 入库单-->
			<view class="report tn-text-md" v-for="report,index in list" :key="report.id">
				<view class="">
					<view class="row">
						<view class="value tn-text-bold tn-text-lg">
							NO.{{report['序号'] }}
						</view>
					</view>
					<view class="row">
						<view class="value">
							品号：{{report['产品编号'] }}
						</view>
						<view class="value">
							品名：{{report['产品名称'] }}
						</view>
					</view>
					<view class="row">
						<view class="value">
							累计入库：{{report['累计入库'] }}
						</view>
						<view class="value">
							工单量：{{report['生产数量'] }}
						</view>

					</view>
					<view class="row">
						<view class="value">
							库存 ：{{report['库存'] }}
						</view>

					</view>
				</view>
				<block>
					<!-- 编辑-->
					<tn-form :labelWidth="120" :model="report" ref="uForm" :errorType="['message']">
						<view class="tn-bg-white ">
							<tn-form-item label="入库量" prop="入库量">
								<tn-input :max-length='8' v-model="report.goodsnum" type="Number"
									:disabled="putin['审核']=='1'" placeholder="输入良品数量"></tn-input>
							</tn-form-item>
							<tn-form-item label="仓库" prop="badtype">
								<tn-input v-model="report.ckcode" type="select" :selectOpen="badshow"
									@click="badchange(index)" placeholder='选择仓库' />
							</tn-form-item>
							<tn-form-item label="仓位" prop="仓位">
								<tn-input v-model="report.cwcode" type="Number" placeholder="输入仓位编码"
									:disabled="putin['审核']=='1'"></tn-input>
								<view class="tn-margin-right-xs" slot="right" v-if="putin['审核']!=1">
									<text class="tn-icon-scan tn-color-blue tn-text-xxl"
										@click="scanCode(index)"></text>
								</view>
							</tn-form-item>
						</view>
					</tn-form>
				</block>
				<view class="btn">
					<tn-button
						v-if="report['入库数量'] !=report.goodsnum || report['仓位'] != report.cwcode || report['仓库'] != report.ckcode"
						fontColor="#fff" @click="editone(report)" backgroundColor="#ffaa00" class="tn-flex-1 bt"
						height="70rpx">保存修改</tn-button>
					<print-btn v-else @print="print" :pdata="report">打印</print-btn>
				</view>
			</view>

		</view>
		<tn-select labelName="仓库名称" valueName="仓库编号" v-model="badshow" :list="cklist" @confirm="ckconfirm"></tn-select>
		<tn-empty v-if="list.length<1" class="error" icon="/static/error/common.png" mode="data"></tn-empty>
		<view class='tn-tabbar-height'></view>

		<bottom-btn v-if="putin['日报编号']" :pdata="putin" @edit="edit" @verify="verify" @save="save" @print="printAll"
			:status="putin['审核']"></bottom-btn>

	</app-layout>
</template>

<script>
	import locationmixin from '@/libs/mixin/location_mixin.js'
	import SearchTop from '@/components/SearchTop'
	import {
		mapGetters,
		mapState
	} from 'vuex';
	export default {
		name: 'PUTIN_ADD',
		mixins: [locationmixin],
		components: {
			SearchTop
		},
		computed: {
			...mapState({
				userinfo: state => state.userinfo
			})
		},
		data() {
			return {
				title: '新增',
				putin: {
					order_no: this.$utils.getNo(0),
					send_no: '',
					user: '',
					date: this.$utils.getAgoDay(0),
				},
				status: '',
				searchForm: {},
				more: false,

				RunType: 0,
				//拍照用的
				formData: {},

				list: [],

				//类型
				badshow: false,
				activeIndex: '',

				cklist: [],

				form: {
					goodsnum: 0,
				},

			}
		},
		onLoad(options) {
			console.log(options)
			let {
				purchase_no
			} = options
			if (purchase_no) this.searchForm.work_number = purchase_no
			this.cklist = this.$storage.getStorageSync('cklist')

		},
		onReady() {},
		onShow() {
			let t = this;
			if (!t.userinfo) t.$user.getInfo()
		},
		methods: {
			badchange(idx) {
				if (this.putin['审核'] == '1') return;
				this.activeIndex = idx;
				this.badshow = true;
			},
			ckconfirm(e) {
				this.list[this.activeIndex].ck = e[0].value;
				this.list[this.activeIndex].ckcode = e[0].label;
			},

			print() {
				console.log('print')
			},
			edit() {
				console.log('edit')
			},
			editone(report) {
				console.log('editone', report)
				let that = this;
				uni.showModal({
					title: "提示",
					content: '确认修改单据?',
					success(e) {
						if (e.confirm) {
							that.$t.message.loading("提交中");
							that.$request({
								url: that.$api.goodsin.editNum,
								data: {
									ie_sub_id: report.ID, //	字符串	必须			入库子表ID
									ie_rk_num: report.goodsnum || 0,
									ckcode: report.ckcode,
									cwcode: report.cwcode,
								},
								method: "POST",
							}).then(response => {
								that.$t.message.closeLoading();

								uni.showModal({
									title: '提示',
									content: response.msg || '操作成功',
									showCancel: false,
									success: function(res) {
										if (res.confirm) {
											console.log('用户点击确定');
											that.getList(1)
										}
									}
								});
							}).catch((msg) => {
								that.$t.message.toast(msg);
								that.$t.message.closeLoading();
							});
						}
					}
				})
			},
			verify() {
				console.log('verify')
				let that = this;
				let r = that.list.find(li => li.goodsnum != li["入库数量"])
				if (r) {
					uni.showToast({
						title: '您还有未保存的修改记录 请先保存',
						icon: 'none'
					})
					return false;
				}
				uni.showModal({
					title: "提示",
					content: '确认审批通过该单据?',
					success(e) {
						if (e.confirm) {
							that.$t.message.loading("提交中");
							that.$request({
								url: that.$api.goodsin.editStatus,
								data: {
									ie_no: that.searchForm.work_number, //字符串	必须			入库单号
									status: 1 //字符串	必须			状态 1已审 2未审
								},
								method: "POST",
							}).then(response => {
								that.$t.message.closeLoading();
								uni.showModal({
									title: '提示',
									content: response.msg || '操作成功',
									showCancel: false,
									success: function(res) {
										if (res.confirm) {
											console.log('用户点击确定');
											uni.redirectTo({
												url: "/mes/putin/list"
											})
										}
									}
								});
							}).catch((msg) => {
								that.$t.message.toast(msg);
								that.$t.message.closeLoading();
							});
						}
					}
				})

			},
			save() {
				console.log('save')
				let that = this;
				if (that.putin['审核'] == 0) {
					uni.showModal({
						title: "提示",
						content: '该订单还未审核，请联系主管审核',
						showCancel: false,
					})
				}
			},
			printAll() {
				console.log('printAll')
			},
			scanCode(index) {
				let that = this;
				that.$jwx.scanQRCode().then(res => {
					that.list[index].ckcode = res
				})

			},

			Search(e) {
				console.log(e)
				this.searchForm = e;
				this.getList(1)
			},


			getList(issearch = 0) {
				let that = this;
				that.$t.message.loading("加载中");
				console.log(that.cklist)
				if (issearch) that.page = 1, that.list = [];
				that.$request({
					url: that.$api.goodsin.detail,
					data: {
						limit_page: that.page,
						limit_count: 20,
						ie_no: that.searchForm.work_number || '',
					}
				}).then(response => {
					that.$t.message.closeLoading();
					if (!response.info) {
						that.putinit()
					}
					let reslist = [];
					if (response.list.length > 0) {
						response.list.forEach(e => {
							reslist.push({
								...e,
								goodsnum: e['入库数量'],
								ckcode: e['仓库'],
								cwcode: e['仓位']
							})
						})
					}
					that.putin = {
						...that.putin,
						...response.info
					}
					if (that.page !== 1) {
						that.list = that.list.concat(reslist);
					} else {
						that.list = reslist;
					}
					that.page = reslist.length ? that.page + 1 : that.page;
				}).catch((msg) => {
					that.$t.message.toast(msg);
					that.$t.message.closeLoading();
				});
			},
			putinit() {
				this.searchForm.work_number = '';
				this.list = [];
				this.putin = {
					send_no: '',
					date: this.$utils.getAgoDay(0),
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.list {}

	.topForm {
		position: relative;
	}

	.tn-form-item {
		padding: 0;
	}

	.nav-height {
		height: 360upx;
	}

	.btn {
		padding-top: 20upx;
		padding-top: 10px;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.bt {
			margin: 0 10upx;
		}
	}



	.report {
		background: #fff;
		padding: 20upx;
		border: 1upx solid #eee;
		margin: 10upx;
		border-radius: 10upx;
		box-shadow: 1px 1px 0px 0px #aba7a7;
		position: relative;


		.row {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 10upx 0;
		}

		.value {
			min-width: 50%;
			overflow: hidden;
			white-space: nowrap;
			text-overflow: ellipsis;
		}

		.tn-form-item {
			padding: 0;
		}
	}

	.right {
		width: 32%;
		display: flex;
		justify-content: space-evenly;
		align-content: center;
		flex-wrap: wrap;

		.more {
			font-size: 40upx;
			display: flex;
			align-items: center;
		}
	}

	.userinfo {
		display: flex;
		font-size: 26upx;
		align-items: center;
		padding: 0 10px;
	}

	.search {
		background-color: #fff;
		padding: 10px;



		.search-btn {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding-top: 20upx;
			border-top: 2px solid #eee;
		}
	}

	/* 底部安全边距 start*/
	.tn-tabbar-height {
		min-height: 120rpx;
		height: calc(140rpx + env(safe-area-inset-bottom) / 2);
		height: calc(140rpx + constant(safe-area-inset-bottom));
	}
</style>